package com.rc.saas.mini.ali;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.internal.util.codec.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.UUID;

/**
 * @Author: zcg
 * @CreateDate: 2022/5/23 16:41
 */
public class YmUtil {
    private static Logger logger = LogManager.getLogger(YmUtil.class);

    public static void main(String[] args) throws Exception {
        System.out.println(getPhoneByYoumengToken("123456"));
    }


    public static String getPhoneByYoumengToken(String youmengToken) {
        //这是你阿里云上面的appkey
        //String umAppkey = "";
        //String appKey = "204065091";
        //String appSecret = "5ukzwDv8fmK24jMs2TtN2s0tPItcQ4Si";
        String APP_KEY = "204065091";
        //这是你友盟上面的appkey
        String YOUMENG_APP_KEY = "62887b9305844627b58aeaec";
        HttpClient httpClient;
        HttpPost postMethod;
        HttpResponse response;
        String reponseContent = null;
        //Body内容
        String body = " {\"token\":\"" + youmengToken + "\"}";

        Date d = new Date();
        String timestamp = String.valueOf(d.getTime());
        String uuid = UUID.randomUUID().toString();

        try {
            httpClient = HttpClients.createDefault();
            postMethod = new HttpPost("http://verify5.market.alicloudapi.com/api/v1/mobile/info?appkey=" + YOUMENG_APP_KEY);
            //设置请求头
            postMethod.addHeader("Content-type", "text/plain;charset=utf-8");
            postMethod.addHeader("Authorization", "APPCODE c84b65208dfc492ab74e4e7a7b8efaae");
            postMethod.addHeader("X-Ca-Timestamp", timestamp);
            postMethod.addHeader("gateway_channel", "http");
            postMethod.addHeader("X-Ca-Key", APP_KEY);
            postMethod.addHeader("X-Ca-Nonce", uuid);
            postMethod.addHeader("X-Ca-Request-Mode", "DEBUG");
            postMethod.addHeader("X-Ca-Stage", "RELEASE");
            postMethod.addHeader("Host", "verify5.market.alicloudapi.com");
            postMethod.addHeader("Content-MD5", Base64.encodeBase64String(DigestUtils.md5(body)));
            postMethod.addHeader("Content-Type", "application/octet-stream; charset=UTF-8");
            postMethod.addHeader("Accept", "application/json");
            postMethod.addHeader("X-Ca-Signature-Headers", "X-Ca-Key,X-Ca-Nonce,X-Ca-Signature,X-Ca-Signature-Headers,X-Ca-Stage,X-Ca-Timestamp");
            postMethod.setEntity(new StringEntity(body, Charset.forName("UTF-8")));
            response = httpClient.execute(postMethod);
            HttpEntity httpEntity = response.getEntity();
            reponseContent = EntityUtils.toString(httpEntity);
            logger.info("友盟：{}", reponseContent);
            if ("true".equals(JSONObject.parseObject(reponseContent).getString("success"))) {
                return JSONObject.parseObject(reponseContent).getJSONObject("data").getString("mobile").toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
